package com.example.back.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.back.entity.Criterion;
import com.example.back.entity.ExpertRecord;
import com.example.back.entity.ProjectFile;
import com.example.back.entity.SelfRecord;
import com.example.back.mapper.CriterionMapper;
import com.example.back.mapper.ExpertRecordMapper;
import com.example.back.mapper.ProjectFileMapper;
import com.example.back.mapper.RecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Jm
 * @date 2024/7/16
 */
@Service
public class CriterionService extends ServiceImpl<CriterionMapper, Criterion>
{
    @Resource
    private CriterionMapper criterionMapper;

    @Resource
    private RecordMapper recordMapper;

    @Resource
    private ExpertRecordMapper expertRecordMapper;

    @Resource
    private ProjectFileMapper projectFileMapper;

    public List<Criterion> getList()
    {
        return list();
    }

    public void addCriterion(Criterion criterion)
    {
        criterionMapper.insert(criterion);
    }

    public List<Criterion> selectTree(Integer id, Integer userId)
    {
        // 获取所有一级指标
        QueryWrapper<Criterion> level1Qw = new QueryWrapper<Criterion>();
        level1Qw.eq("template_id", id);
        level1Qw.eq("level", 1);
        List<Criterion> result = criterionMapper.selectList(level1Qw);
        for(Criterion criterion : result)
        {
            // 查询所有一级指标对应的二级指标
            QueryWrapper<Criterion> level2Qw = new QueryWrapper<Criterion>();
            level2Qw.eq("parent", criterion.getId());
            List<Criterion> level2 = criterionMapper.selectList(level2Qw);

            for(Criterion criterion1 : level2)
            {
                //查询所有二级指标对应的三级指标
                QueryWrapper<Criterion> level3Qw = new QueryWrapper<Criterion>();
                level3Qw.eq("parent", criterion1.getId());
                List<Criterion> level3 = criterionMapper.selectList(level3Qw);
                //查询指标对应的佐证材料
                for(Criterion criterion4: level3)
                {
                    QueryWrapper<ProjectFile> fileQueryWrapper = new QueryWrapper<ProjectFile>();
                    fileQueryWrapper.eq("criterion_id", criterion4.getId());
                    fileQueryWrapper.eq("user_id", userId);
                    List<ProjectFile> files = projectFileMapper.selectList(fileQueryWrapper);
                    criterion4.setFiles(files);
                }
                criterion1.setChild(level3);
            }
            criterion.setChild(level2);
        }
        return result;
    }


    public List<Criterion> selectScoreTree(Integer id, Integer schoolId)
    {
        // 获取所有一级指标
        QueryWrapper<Criterion> level1Qw = new QueryWrapper<Criterion>();
        level1Qw.eq("template_id", id);
        level1Qw.eq("level", 1);
        List<Criterion> result = criterionMapper.selectList(level1Qw);
        for(Criterion criterion : result)
        {
            // 查询所有一级指标对应的二级指标
            QueryWrapper<Criterion> level2Qw = new QueryWrapper<Criterion>();
            level2Qw.eq("parent", criterion.getId());
            List<Criterion> level2 = criterionMapper.selectList(level2Qw);

            for(Criterion criterion1 : level2)
            {
                //查询所有二级指标对应的三级指标
                QueryWrapper<Criterion> level3Qw = new QueryWrapper<Criterion>();
                level3Qw.eq("parent", criterion1.getId());
                List<Criterion> level3 = criterionMapper.selectList(level3Qw);

                for(Criterion criterion2: level3)
                {
                    //查询自评分数
                    QueryWrapper<SelfRecord> selfRecordQueryWrapper = new QueryWrapper<SelfRecord>();
                    selfRecordQueryWrapper.eq("school_id", schoolId);
                    selfRecordQueryWrapper.eq("criterion_id", criterion2.getId());
                    SelfRecord record = recordMapper.selectOne(selfRecordQueryWrapper);
                    if(record != null)
                    {
                        criterion2.setScore(record.getScore());
                    }else
                    {
                        criterion2.setScore(null);
                    }
                    //查询佐证材料
                    QueryWrapper<ProjectFile> fileQueryWrapper = new QueryWrapper<ProjectFile>();
                    fileQueryWrapper.eq("criterion_id", criterion2.getId());
                    fileQueryWrapper.eq("user_id", schoolId);
                    List<ProjectFile> files = projectFileMapper.selectList(fileQueryWrapper);
                    criterion2.setFiles(files);
                }
                criterion1.setChild(level3);
            }
            criterion.setChild(level2);
        }
        return result;
    }

    public List<Criterion> selectAllScore(Integer id, Integer schoolId)
    {
        // 获取所有一级指标
        QueryWrapper<Criterion> level1Qw = new QueryWrapper<Criterion>();
        level1Qw.eq("template_id", id);
        level1Qw.eq("level", 1);
        List<Criterion> result = criterionMapper.selectList(level1Qw);
        for(Criterion criterion : result)
        {
            // 查询所有一级指标对应的二级指标
            QueryWrapper<Criterion> level2Qw = new QueryWrapper<Criterion>();
            level2Qw.eq("parent", criterion.getId());
            List<Criterion> level2 = criterionMapper.selectList(level2Qw);

            for(Criterion criterion1 : level2)
            {
                //查询所有二级指标对应的三级指标
                QueryWrapper<Criterion> level3Qw = new QueryWrapper<Criterion>();
                level3Qw.eq("parent", criterion1.getId());
                List<Criterion> level3 = criterionMapper.selectList(level3Qw);

                for(Criterion criterion2: level3)
                {
                    //查询自评分数
                    QueryWrapper<SelfRecord> selfRecordQueryWrapper = new QueryWrapper<SelfRecord>();
                    selfRecordQueryWrapper.eq("school_id", schoolId);
                    selfRecordQueryWrapper.eq("criterion_id", criterion2.getId());
                    SelfRecord record = recordMapper.selectOne(selfRecordQueryWrapper);
                    if(record != null)
                    {
                        criterion2.setScore(record.getScore());
                    }

                    //查询评估组成绩
                    QueryWrapper<ExpertRecord> expertRecordQueryWrapper = new QueryWrapper<ExpertRecord>();
                    expertRecordQueryWrapper.eq("school_id", schoolId);
                    expertRecordQueryWrapper.eq("criterion_id", criterion2.getId());
                    ExpertRecord expertRecord = expertRecordMapper.selectOne(expertRecordQueryWrapper);
                    if(expertRecord != null)
                    {
                        criterion2.setExpertScore(expertRecord.getScore());
                    }

                    //查询佐证材料
                    QueryWrapper<ProjectFile> fileQueryWrapper = new QueryWrapper<ProjectFile>();
                    fileQueryWrapper.eq("criterion_id", criterion2.getId());
                    fileQueryWrapper.eq("user_id", schoolId);
                    List<ProjectFile> files = projectFileMapper.selectList(fileQueryWrapper);
                    criterion2.setFiles(files);

                }
                criterion1.setChild(level3);
            }
            criterion.setChild(level2);
        }
        return result;
    }
}
